import json
from datetime import datetime

from django.core.paginator import Paginator
from django.shortcuts import render

from apps.aiauth.models import Users
from apps.job.serializers import ApplyjobSerializers
from utils import restful
from .models import Applyjob, Interviews


def getAppllyjobList(request):
    pagenow = request.GET.get('pagenow', 1)
    size = request.GET.get('pagesize', 5)
    user = request.GET.get('user_id')
    organization_id = request.GET.get('organization_id')
    realName = request.GET.get('realName')
    student_id = request.GET.get('student_id')
    search_dict = {}
    if user:
        search_dict['user'] = user
    if organization_id:
        search_dict['user__userinfo__organization_id'] = organization_id
    if realName:
        search_dict['user__realname__iexact'] = realName
    if student_id:
        search_dict['user__studentid'] = student_id
    applyjobList = Applyjob.objects.filter(
        **search_dict,
    ).order_by('-create_time')

    page_obj = Paginator(applyjobList, size)
    page_data = page_obj.get_page(pagenow)
    totalcount = page_obj.count

    data = ApplyjobSerializers(page_data.object_list, many=True).data

    return restful.result(message="成功", data=data, kwargs={'totalcount': totalcount})


def saveApplyjob(request):
    userid = request.POST.get('userid')
    user = Users.objects.get(user_id=userid)
    print(user)
    job_type = request.POST.get('jobType')
    job_name = request.POST.get('jobName')
    applyTime = request.POST.get('applyTime')
    interviewsList = request.POST.get('interviewsList')
    if (applyTime == ''):
        applyTime = None
    applyChannel = request.POST.get('applyChannel')
    offer = request.POST.get('offer')
    if offer == 'true':
        offer = True
    else:
        offer = False
    summary = request.POST.get('summary')
    company = request.POST.get('company')
    company_contact = request.POST.get('companyContact')
    applyjob = Applyjob.objects.create(
        job_name=job_name,
        apply_time=applyTime,
        apply_channel=applyChannel,
        offer=offer,
        summary=summary,
        company=company,
        company_contact=company_contact,
        create_time=datetime.now(),
        job_type=job_type,
        user=user
    )
    # 将面试数据取出，存入数据库
    for interview in json.loads(interviewsList):
        if interview['time'] == '':
            interview['time'] = None
        Interviews.objects.create(
            applyjob=applyjob,
            create_time=datetime.now(),
            time=interview['time'],
            content=interview['content']
        )

    data = ApplyjobSerializers(applyjob).data

    return restful.ok(message="保存成功", data=data)


def applyjob(request):
    if request.is_ajax():
        # user_id = request.session.get('user_id')
        user_id = request.POST.get('user_id')
        messages = Applyjob.objects.filter(user=user_id)
        data = ApplyjobSerializers(messages, many=True).data

        return restful.ok(data=data)

    return render(request, 'job/applyjob.html')
